<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir."/inc/table.php";
require_once $basedir."/inc/gruppen.php";
require_once $basedir."/inc/kinder.php";

class GruppenKinder extends table {
    
    public static $TABLENAME = "gruppenkinder";
    public static $CLASSNAME = "GruppenKinder";
    
	private $id="";
	private $gruppen_id;
	private $kind_id;

    public function __construct($id="") {
		if($id!='') {
            $temp = Database::getDefault()->openId(GruppenKinder::$TABLENAME,$id);
            foreach($temp as $key => $value) {
                $this->$key = $value;
            }
		}
    }
    
    public function delete() {
        Database::getDefault()->delete(GruppenKinder::$TABLENAME,$this->id);
    }
    
    public function save() {
    	$values = array();
		foreach ($this as $key => $value) {
			$values[$key] = $value;
		}
        if($this->id=="") {
            $this->id = Database::getDefault()->insert(GruppenKinder::$TABLENAME, $values);
        } else {
            Database::getDefault()->update(GruppenKinder::$TABLENAME, $values, $this->id);
        }
    }
	
    /**
     * List alle Gruppen / Kinder Zuordnungen
     * 
     * @param aktuell Standard true, es werden nur die jetzt aktiven Verträge betrachtet
     * @param $gruppen_id optional nur wenn eine GruppenID mitgegeben wird, werden die
     *                    Kinder von nur dieser Gruppe zurückgeholt
     * @param $date optional kann das zu verwendene Datum für aktuell mitgegeben werden.
     *              das format ist dann yyyy-MM-dd und aktuell ist dann immer true.
     * @return Array von GruppenKinder Objecten
     */
    static public function getAll($aktuell=true,$gruppen_id="",$date="") {
        global $VEREIN;
        if($date!="") {
            $aktuell = true;
        } else {
            $date = date("Y-m-d");
        }
        $query = "SELECT DISTINCT GruppenKinder.* FROM " . $VEREIN['prefix'] . GruppenKinder::$TABLENAME . " AS GruppenKinder, " . $VEREIN['prefix'] . Vertraege::$TABLENAME . " AS Vertraege, " . $VEREIN['prefix'] . Kinder::$TABLENAME . " AS Kinder WHERE";
		if($aktuell) {
			$query .= " (Vertraege.vertrag_ab <= '".$date."') AND (Vertraege.ausgeschieden_am = '0000-00-00' OR Vertraege.ausgeschieden_am > '".$date."') AND GruppenKinder.kind_id = Vertraege.kind_id";
			if($gruppen_id!="") {
				$query .= " AND";
			}
		}
		if($gruppen_id!="") {
			$query .= " GruppenKinder.gruppen_id='$gruppen_id'";
		}
        if($aktuell || $gruppen_id!="") {
            $query .= " AND";
        }
        $query .= " Kinder.id=Vertraege.kind_id ORDER BY Kinder.nachname, Kinder.vorname";
        return Database::getDefault()->execute($query,GruppenKinder::$CLASSNAME);
    }
	
	public function getId() {
		return $this->id;
	}
	
	public function setGruppenId($value) {
		$this->gruppen_id = $value;
	}
	
	public function getGruppenId() {
		return $this->gruppen_id;
	}
	
	public function getGruppe() {
		return new Gruppen($this->gruppen_id);
	}
	
	public function setKindId($value) {
		$this->kind_id = $value;
	}
	
	public function getKindId() {
		return $this->kind_id;
	}
	
	public function getKind() {
		return new Kinder($this->kind_id);
	}
    
    /**
     * create table for banking contacts or update if something to do
     */
    public static function initDatabase() {
        global $VEREIN;
        echo "<b>update / create " . GruppenKinder::$TABLENAME . "</b><br />";
        $query = "CREATE TABLE IF NOT EXISTS " . $VEREIN['prefix'] . GruppenKinder::$TABLENAME . " (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY)";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            die(Database::getDefault()->getConnection()->error);
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . GruppenKinder::$TABLENAME . " ADD COLUMN gruppen_id INT UNSIGNED DEFAULT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . GruppenKinder::$TABLENAME . " ADD COLUMN kind_id INT UNSIGNED DEFAULT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        echo "<br />";
    }
		
}
?>